package utils;


import icons.ResLoader;

import java.awt.Component;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

public class CreateImage {

	private static BufferedImage image;

	/** Returns an ImageIcon, or null if the path was invalid. */
	public static ImageIcon createImageIcon(String path) {
		java.net.URL imgURL = ResLoader.class.getResource(path);
		if (imgURL != null) {
			return new ImageIcon(imgURL);
		} else {
			System.err.println("Couldn't find file: " + path);
			System.exit(1);
			return null;
		}
	}
	
	public static ImageIcon getScaled(String filename, double scale, boolean end){
		Image image = loadImage(filename, end);
		return getScaledIcon(image, scale);
	}

	private static Image loadImage(String fileName, boolean end) {
//		if(!end){
//			fileName = "icons/icons" + fileName;
//		}
		try {
			URL url = ResLoader.class.getResource(fileName);
			image = ImageIO.read(url);
		} catch (MalformedURLException mue) {
			System.out.println("bad URL: " + mue.getMessage());
		} catch (IOException ioe) {
			System.out.println("io help: " + ioe.getMessage());
		}
		return image;
	}

	private static ImageIcon getScaledIcon(final Image image, final double scale) {
		ImageIcon scaledIcon = new ImageIcon(image) {
			private static final long serialVersionUID = -2880416042502264971L;

			public int getIconWidth() {
				return (int) (image.getWidth(null) * scale);
			}

			public int getIconHeight() {
				return (int) (image.getHeight(null) * scale);
			}

			public void paintIcon(Component c, Graphics g, int x, int y) {
				g.drawImage(image, x, y, getIconWidth(), getIconHeight(), c);
			}
		};
		return scaledIcon;
	}

}
